/*
 *所有关于Attendance_Records类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Attendance_RecordsService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Dhj.IRepositories;
using VOL.Core.DBManager;
using VOL.Core.Enums;
using VOL.Core.ManageUser;
using System.Collections.Generic;
using System;
using System.Net;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System.Collections;
using System.Text;

namespace VOL.Dhj.Services
{
    public partial class Attendance_RecordsService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IAttendance_RecordsRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Attendance_RecordsService(
            IAttendance_RecordsRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }


        //查询
        public override PageGridData<Attendance_Records> GetPageData(PageDataOptions options)
        {

            QueryRelativeExpression = (IQueryable<Attendance_Records> queryable) =>
            {
                var dapper = DBServerProvider.SqlDapper;
                //获取用户信息
                int userAccount = UserContext.Current.UserInfo.Role_Id;
                string userTrueName = UserContext.Current.UserInfo.UserTrueName;//用户权限等级
                string userName = UserContext.Current.UserInfo.UserName;//登录的用户名

                //Console.WriteLine("用户信息" + userName);
                

                //获取用户权限
                Permissions a = UserContext.Current.GetPermissions(x => x.TableName.ToLower() == "attendance_records");

                Boolean pw = false;//初始设置为没有访问权限
                Array.ForEach(a.UserAuthArr, item =>
                {
                    //当用户存在私人查询权限时，只能查到用户自己的信息
                    if (item.Equals("public_query"))
                    {
                        pw = true;//开放权限                                               
                    }
                    else
                    {
                        if (!pw) {
                            pw = false;
                        }
                                               
                    }
                });

                if (pw == true)
                {
                    //有权限，查询全部的用户信息
                    string sql = "SELECT * from Attendance_Records ";
                    List<Attendance_Records> list11 = dapper.QueryList<Attendance_Records>(sql, true);
                }
                else {
                    //没有权限，只能查询用户本身的信息                   
                    //queryable = queryable.Where(x => x.StaffAccount == userName);

                    queryable = queryable.Where(x => x.Manager == "孔凡军");
                }

                return queryable;
            };            
            
            return base.GetPageData(options);
        }


        
    }
}
